using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Elementum
{
    class SmokePlumeParticleSystem:ParticleSystem
    {
        public SmokePlumeParticleSystem(Stage stage,int h) : base(stage,h) { }

        protected override void InitializeConstants()
        {
            textureFilename = "images/smoke";

            minInitialSpeed = 5;
            maxInitialSpeed = 12;

            // we don't want the particles to accelerate at all, aside from what we
            // do in our overriden InitializeParticle.
            minAcceleration = 0;
            maxAcceleration = 0;

            // long lifetime, this can be changed to create thinner or thicker smoke.
            // tweak minNumParticles and maxNumParticles to complement the effect.
            minLifetime = 6.0f;
            maxLifetime = 10.0f;

            minScale = 0.8f;
            maxScale = 1.8f;

            minNumParticles =10;
            maxNumParticles = 20;


            // rotate slowly, we want a fairly relaxed effect
            minRotationSpeed = -MathHelper.PiOver4 / 2.0f;
            maxRotationSpeed = MathHelper.PiOver4 / 2.0f;

            blendState = BlendState.AlphaBlend;

            //DrawOrder = AlphaBlendDrawOrder;
        }

        protected override Vector2 PickRandomDirection()
        {
            // Point the particles somewhere between 80 and 100 degrees.
            // tweak this to make the smoke have more or less spread.
            float radians = GameClass.RandomBetween(0,2*PI);

            Vector2 direction = Vector2.Zero;
            // from the unit circle, cosine is the x coordinate and sine is the
            // y coordinate. We're negating y because on the screen increasing y moves
            // down the monitor.
            direction.X = (float)Math.Cos(radians);
            direction.Y = (float)Math.Sin(radians);
            return direction;
        }

        public void BlowAway(Vector2 from) {
            foreach (Particle p in particles)
            {
                if (p.Active)
                {
                    Vector2 v = p.Position - from;
                    v.Normalize();
                    p.Velocity = v * 120f;
                    p.Acceleration = v*25f;
                    if (!p.Active)
                    {
                        freeParticles.Enqueue(p);
                    }
                }
            }
        }

        protected override void InitializeParticle(Particle p, Vector2 where)
        {
            base.InitializeParticle(p, where);

            // the base is mostly good, but we want to simulate a little bit of wind
            // heading to the right.
            p.Acceleration.X += GameClass.RandomBetween(-20, 20);
        }

    }
}
